function initiCheck() {
    $("input[type='radio'],input[type='checkbox']").iCheck({
        checkboxClass: 'icheckbox_square-blue',
        radioClass: 'iradio_square-blue',
        increaseArea: '20%'
    });
    $("#checkAll").on("ifClicked", function (e) {
        $(".id").each(function () {
            $(this).iCheck("toggle");
        });
    });
}

function initPassword() {
    $(document).on("submit", "[data-password='true']", function () {
        if ($("#password").val() == "" || ($("#password").val() != $("#repassword").val())) {
            alert("确认密码不一致!");
            return false;
        } else {
            return true;
        }
    });
}

function initPage() {
    $("li.selected").each(function () {
        $(this).addClass("active");
        $(this).children("a").removeAttr("href");
    });
}

function load_exam(url)
{
    $(document).ajaxStart(function(){
        $("#loading").show();
    });
    $(document).ajaxComplete(function(){
        $("#loading").hide();
    });
    $("#exam").load(url);
}
$(document).ready(function () {
    var ajax;
    $(document).on("click", "[data-action='learn']", function () {
        var $this = $(this);
        ajax && ajax.abort();
        var article_id = $this.data("article");
        $this.text("操作中...").prop("disabled",true);
        ajax = $.get('/article/learn',{id:article_id},function(data){
         if(data == 1){
             $this.after('<p class="text-success">成功加入我的课程列表</p>').remove();
          }else{
             $this.text("操作失败,请刷新后重试");
         }
        });
    });
    $(document).on("click","[data-action='reply']",function(){
       var pid = $(this).data("pid");
        var title = $(this).data("title");
        var form = $("#reply-form");
        form.find(".form-control-static").text("回复:@"+title);
        form.find("#pid").val(pid);
        $(this).after(form.show());
        $(this).remove();
    });
});

function initList()
{
    var ajax;
    $("li.selected").each(function() {
        $(this).addClass("active");
        $(this).children("a").removeAttr("href");
    });
    $(document).on("click", "#checkAll", function() {
        $(".id").each(function() {
            this.checked = !this.checked;
        });
    });
    $(document).on("click", ".btn-group button", function() {
        var $this = $(this);
        var url = $(this).attr("data-url");
        var selected = [];
        $(".id").each(function() {
            if (this.checked) {
                selected.push(this.value);
            }
        });
        if (selected.length > 0) {
            if($this.text() == "删除"){
                if(confirm('确定删除吗？')){
                    ajax && ajax.abort();
                    ajax = $.post(url, {list: selected}, function(data) {
                        if (data == 1) {
                            window.location.reload();
                        }
                        else {
                            alert("操作失败!");
                        }
                    });
                }
            }else{
                ajax && ajax.abort();
                ajax = $.post(url, {list: selected}, function(data) {
                    if (data == 1) {
                        window.location.reload();
                    }
                    else {
                        alert("操作失败!");
                    }
                });
            }
        }
    });
}

function initUM(editor)
{
    UM.getEditor(editor,{toolbar:['bold italic underline strikethrough | justifyleft justifycenter justifyright | insertorderedlist insertunorderedlist ' +
        'cleardoc selectall link unlink image | removeformat horizontal | fullscreen preview print']});
}